package com.csw.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo07Combine {
    public static class CombineMap extends Mapper<LongWritable,Text,Text,IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String clazz = line.split(",")[4];
            context.write(new Text(clazz),new IntWritable(1));
        }
    }
    //Combine语聚合中输入kv和输出kv要保持一致
    //在map之后执行，并且在map端进行预聚合，Combine 由map task决定
    // 有多少个map task就有多少个Combine预聚合
    public static class Combine extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum=0;
            for (IntWritable value : values) {
                int i = value.get();
                sum+=i;
            }
            String clazz = key.toString();
            context.write(new Text(clazz),new IntWritable(sum));
        }
    }
    public static class CombineReduce extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum=0;
            for (IntWritable value : values) {
                int i = value.get();
                sum+=i;
            }
            String clazz = key.toString();
            context.write(new Text(clazz),new IntWritable(sum));
        }
    }
    public static void main(String[] args) throws Exception {
        //构建一个job
        Job job = Job.getInstance();
        //指定job任务名称(不指定也可以 为系统默认的名称)
        job.setJobName("combine");
        //指定当前main所在类名
        job.setJarByClass(Demo07Combine.class);

        //指定map类
        job.setMapperClass(CombineMap.class);
        //指定map输出的k和v的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //指定Combine预聚合
        job.setCombinerClass(Combine.class);
        //指定reduce类
        job.setReducerClass(CombineReduce.class);
        //指定reduce输出的k和v的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //获取fileSystem对象，hdfs文件管理系统的对象
        FileSystem fs = FileSystem.get(new Configuration());
        Path path = new Path("/data/combine");
        //指定输出路径(路径不能已存在，存在需要删除)
        if (fs.exists(path)){
            fs.delete(path,true);
        }
        //指定输入路径
        FileInputFormat.addInputPath(job,new Path("/data/students.txt"));

        //指定输出路径
        FileOutputFormat.setOutputPath(job,path);
        //启动
        job.waitForCompletion(true);
    }
}
